<template>
  <view class="page-bg">
    <!-- 企业信息卡 -->
    <view class="company-card">
      <view class="company-title">
        {{commanyInfo.enterpriseName}}
        <text class="company-auth">已认证企业</text>
      </view>
      <view class="company-code">统一社会信用代码：{{commanyInfo.enterpriseCode}}</view>
    </view>

    <!-- 填写指南 -->
    <view class="guide-card">
      <view class="guide-title">
        <u-icon name="info-circle-fill" color="#409eff" size="20" style="margin-right:6px;" />
        填写指南
      </view>
      <view class="guide-list">
        <view>• 企业简介将用于协会官网展示及会员间信息交流</view>
        <view>• 内容需包含企业成立时间、主营业务、规模、核心优势等关键信息</view>
        <view>• 文字长度限制为300-1500字，超出范围将无法提交</view>
        <view>• 请使用正式、规范的表述，避免使用营销性质过强的词汇</view>
        <view>• 提交后将经过审核，审核通过后将在24小时内更新展示</view>
      </view>
    </view>

    <!-- 企业简介内容 -->
    <view class="form-card">
      <view class="form-label">
        企业简介内容
        <text class="required">(必填)</text>
      </view>
      <view class="textarea-wrap">
        <textarea
          v-model="intro"
          :maxlength="1500"
          placeholder="请输入企业简介内容: \n 例如：\n\n 华星电子科技有限公司成立于2010年，是一家专注于智能硬件研发与生产的高新技术企业。公司总部位于深圳，占地面积5000平方米，现有员工300余人... \n \n 主要产品包括... \n 公司拥有多项专利技术，通过ISO9001质量管理体系认证，产品远销全球多个国家和地区..."
          class="intro-textarea"
          
          @input="onInput"
        />
      </view>
      <view class="form-footer">
        <text :class="[(intro.length < 300 || intro.length > 1500) ? 'red' : '']">{{ intro.length }}</text>
        / 1500 字
        <text class="tip" v-if="intro.length < 300 || intro.length > 1500">请输入300-1500字的企业简介</text>
      </view>
    </view>

    <!-- 内容预览 -->
    <view class="preview-card">
      <view class="preview-header">
        <view>内容预览</view>
        <view class="preview-tip">(实际展示效果)</view>
      </view>
      <view class="preview-content">
        <view v-if="intro">
          <rich-text :nodes="formatPreview(intro)" />
        </view>
        <view v-else class="preview-placeholder">
          输入内容后将在此处显示预览效果
        </view>
      </view>
    </view>

    <!-- 固定底部保存按钮 -->
    <view class="fixed-footer">
      <button class="save-btn"  @click="onSave">
        保存
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref ,onMounted} from 'vue'
import {getEnterpriseInfo ,updateEnterprise}from '@/api/index'

let commanyInfo = ref({})
onMounted(()=>{
  getEnterpriseInfo().then(res=>{
    console.log(res);
    if(res.code==200){
      console.log(res);
      commanyInfo.value = res.data
      intro.value=res.data.introduction
    }
  })
})
const intro = ref('')

const onInput = (e) => {
  // 兼容部分平台的事件格式
  intro.value = typeof e === 'string' ? e : e.detail.value
}

const formatPreview = (text) => {
  // 简单换行转<p>，可根据需要优化
  if (!text) return ''
  return text
    .split('\n')
    .map(line => line ? `<p>${line}</p>` : '<br/>')
    .join('')
}

const onSave = () => {
  updateEnterprise({introduction:intro.value}).then(res=>{
    if(res.code==200){
      uni.showToast({
        title: '保存成功',
        icon: 'success'
      })
      setTimeout(()=>{
        uni.navigateBack()
      },1500)
    }else{
      uni.showToast({ title: res.msg, icon: 'none' })
    }
  })

}
</script>

<style scoped>
.page-bg {
  background: #f7f8fa;
  min-height: 100vh;
  padding-bottom: 90px;
}
.company-card {
  background: #fff;
  border-radius: 14px;
  margin: 18px 12px 0 12px;
  padding: 18px 18px 10px 18px;
  box-shadow: 0 2px 12px 0 rgba(64,158,255,0.04);
}
.company-title {
  font-size: 18px;
  font-weight: bold;
  color: #222;
  display: flex;
  align-items: center;
  gap: 10px;
}
.company-auth {
  background: #e6f0ff;
  color: #409eff;
  font-size: 13px;
  border-radius: 12px;
  padding: 2px 12px;
  margin-left: 10px;
}
.company-code {
  color: #b0b3bb;
  font-size: 15px;
  margin-top: 6px;
}
.guide-card {
  background: #f4f8ff;
  border-radius: 14px;
  margin: 18px 12px 0 12px;
  padding: 16px 16px 10px 16px;
}
.guide-title {
  font-size: 16px;
  font-weight: bold;
  color: #222;
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.guide-list view {
  color: #4a4a4a;
  font-size: 14px;
  margin-bottom: 4px;
}
.form-card {
  background: #fff;
  border-radius: 14px;
  margin: 18px 12px 0 12px;
  padding: 16px 16px 10px 16px;
  box-shadow: 0 2px 12px 0 rgba(64,158,255,0.04);
}
.form-label {
  font-size: 16px;
  font-weight: bold;
  color: #222;
  margin-bottom: 8px;
}
.required {
  color: #fa5151;
  font-size: 15px;
  margin-left: 6px;
}
.textarea-wrap {
  border: 1px solid #e5e6eb;
  border-radius: 10px;
  background: #fafbfc;
  padding: 10px;
  margin-bottom: 8px;
}
.intro-textarea {
  width: 100%;
  min-height: 400rpx;
  font-size: 30rpx;
  color: #666;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  white-space: pre-wrap;
}
.form-footer {
  display: flex;
  align-items: center;
  font-size: 15px;
  color: #888;
  margin-top: 2px;
  gap: 8px;
}
.form-footer .red {
  color: #fa5151;
}
.form-footer .tip {
  color: #fa5151;
  margin-left: 10px;
  font-size: 14px;
}
.preview-card {
  background: #fff;
  border-radius: 14px;
  margin: 18px 12px 0 12px;
  padding: 16px 16px 18px 16px;
  box-shadow: 0 2px 12px 0 rgba(64,158,255,0.04);
}
.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  color: #222;
  margin-bottom: 8px;
}
.preview-tip {
  font-size: 14px;
  color: #888;
  font-weight: normal;
}
.preview-content {
  min-height: 80px;
  color: #666;
  font-size: 16px;
  background: #f7f8fa;
  border-radius: 8px;
  padding: 18px 12px;
}
.preview-placeholder {
  color: #b0b3bb;
  font-size: 16px;
}
.fixed-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  box-shadow: 0 -2px 12px 0 rgba(64,158,255,0.04);
  padding: 14px 18px 18px 18px;
  z-index: 99;
}
.save-btn {
  width: 100%;
  background: #2196f3;
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  /* padding: 14px 0; */
  font-weight: bold;
  letter-spacing: 2px;
  /* box-shadow: 0 2px 8px 0 rgba(64,158,255,0.08); */ 
}
.save-btn:disabled {
  background: #e5e6eb;
  color: #b0b3bb;
}
</style>